package com.ooolong.expression.operator;

import com.ooolong.expression.ExpressionException;
import com.ooolong.expression.OObject;

public abstract class Operator extends OObject {

	private String symbol;
	private int precedence;
	private boolean isUnary;

	public static Operator createOperator(String symbol) {
		if (symbol.equals("+")) {
			return new Add();
		} else if (symbol.equals("-")) {
			return new Substract();
		} else if (symbol.equals("*")) {
			return new Multiply();
		} else if (symbol.equals("/")) {
			return new Divide();
		} else if (symbol.equals("%")) {
		} else if (symbol.equals("=")) {
		} else if (symbol.equals(">")) {
		} else if (symbol.equals("<")) {
		} else if (symbol.equals("!=")) {
		} else if (symbol.equals(">=")) {
		} else if (symbol.equals("<=")) {
		} else if (symbol.equals("(")) {
		} else if (symbol.equals(")")) {
		} else if (symbol.equals("[")) {
		} else if (symbol.equals("]")) {
		} else if (symbol.equals("{")) {
		} else if (symbol.equals("}")) {
		}

		return null;
	}

	protected Operator(String symbol, int precedence, boolean isUnary) {
		super();
		this.symbol = symbol;
		this.precedence = precedence;
		this.isUnary = isUnary;
	}

	public String getSymbol() {
		return this.symbol;
	}

	public int getPrecedence() {
		return this.precedence;
	}

	public boolean isUnary() {
		return this.isUnary;
	}

	public String toString() {
		return this.symbol;
	}

	protected void checkArguments(OObject[] args) throws ExpressionException {
		int argLength;
		if (isUnary) {
			argLength = 1;
		} else {
			argLength = 2;
		}
		if (args.length != argLength) {
			throw new ExpressionException();
		}

		for (int i = 0; i < args.length; i++) {
			OObject arg = args[i];
			if (arg == null) {
				throw new ExpressionException();
			}
			if (!arg.isNumber()) {
				throw new ExpressionException();
			}
		}
	}

	abstract public OObject execute(OObject[] args) throws ExpressionException;
}
